package com.control.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.CustomerDetailInfoDAO;
import com.dao.CustomerInfoDAO;
import com.dao.GoodsEvaluateDAO;
import com.dao.OrderInfoDAO;
import com.dao.ServiceAssessmentDAO;
import com.dao.ThirdInfoDAO;
import com.entity.ThirdInfo;
import com.entity.ServiceAssessment;
import com.util.PublicToolCheckParam;
import com.util.PublicToolParam;
import com.util.Validate;

public class DoMethodThird extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 712262377940917206L;
	/**
	 * 校验Email是否存在
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void checkEmail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		String regEmail = Validate
				.replaceHtml(request.getParameter("regEmail"));
		CustomerInfoDAO cdao = new CustomerInfoDAO();
		boolean isRegEmailExist = cdao.checkEmail(regEmail);
		if (isRegEmailExist == true) {
			out.println("该邮箱已被注册!请换个邮箱!");

		} else {
			out.println("该邮箱未被注册!");
		}
	}
	/**
	 * 登录方法
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void checkLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		String email = Validate.replaceHtml(request.getParameter("email"));
		String pwd = Validate.replaceHtml(request.getParameter("pwd"));
		CustomerInfoDAO cdao = new CustomerInfoDAO();
		boolean isRegisted = cdao.checkLogin(email, pwd);
		if (isRegisted == false) {

			request.setAttribute("error", "用户名或密码不正确,请重新输入!");
			request.getRequestDispatcher(PublicToolParam.LOGINORREGISTER)
					.forward(request, response);
		} else {
			boolean isCooled = cdao.isCooled(email);
			if (isCooled == true) {
				request.setAttribute("error", "该账号未被审核!请换用其他账号登陆或等待审核!");
				request.getRequestDispatcher(PublicToolParam.LOGINORREGISTER)
						.forward(request, response);
			} else {
				session.setAttribute("email", email);
				response.sendRedirect("../");
			}
		}

	}
	/**
	 * 退出登录
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void loginOut(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		session.removeAttribute("thirdemail");
		session.removeAttribute(PublicToolParam.SHOPLIST);
		response.sendRedirect("../");

	}
	/**
	 * 注册用户
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@SuppressWarnings("static-access")
	public static void register(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String regEmail = Validate.replaceHtml(request.getParameter("regEmail"));
		String regPwd = Validate.replaceHtml(request.getParameter("regPwd"));
		String thirdName = Validate.replaceHtml(request.getParameter("thirdName"));
		String phone = Validate.replaceHtml(request.getParameter("phone"));
		String zhicheng = Validate.replaceHtml(request.getParameter("zhicheng"));
		String address = Validate.replaceHtml(request.getParameter("address"));
		ThirdInfoDAO cdao = new ThirdInfoDAO();
		cdao.addThird(regEmail, regPwd, thirdName, phone, zhicheng, address);
		response.sendRedirect("../filterWeb/ok1third.jsp");
	}

	/**
	 * Constructor of the object.
	 */
	public DoMethodThird() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String method = Validate.replaceHtml(request.getParameter("method"));
		if (PublicToolCheckParam.checkNullAndEmpty(method)) {
			if (method.equals("checkEmail")) {
				checkEmail(request, response);
			} else if (method.equals("checkLogin")) {
				checkLogin(request, response);
			} else if (method.equals("register")) {
				register(request, response);
			} else if (method.equals("loginOut")) {
				loginOut(request, response);
			}
			/*
			 * else if(method.equals("showMyInformation")){
			 * showMyInformation(request, response); }
			 */
		}
	}

	/*
	 * @SuppressWarnings("static-access") public static void
	 * showMyInformation(HttpServletRequest request, HttpServletResponse
	 * response) throws ServletException, IOException {
	 * 
	 * PrintWriter out = response.getWriter(); HttpSession session =
	 * request.getSession(); String email =
	 * (String)session.getAttribute("email"); if(email != null){ ThirdInfoDAO
	 * cdao = new ThirdInfoDAO(); ThirdInfo ThirdInfo =
	 * cdao.getThirdInfo(email); int thirdId = ThirdInfo.getId();
	 * 
	 * // CustomerDetailInfoDAO cddao = new CustomerDetailInfoDAO(); //
	 * CustomerDetailInfo customerDetailInfo =
	 * cddao.getCustomerDetailInfo(customerId);
	 * 
	 * OrderInfoDAO odao = new OrderInfoDAO(); ArrayList<Object> orderList =
	 * odao.getOrderInfoByCustomerId(customerId);
	 * 
	 * GoodsEvaluateDAO gedao = new GoodsEvaluateDAO(); ArrayList<Object>
	 * goodsEvaluateList = gedao.selectGoodsEvaluateInfoByEmail(email);
	 * 
	 * ServiceAssessmentDAO sdao = new ServiceAssessmentDAO(); String isInsert =
	 * Validate.replaceHtml(request.getParameter("isInsert"); if(isInsert !=
	 * null){ if(isInsert.equals("insert")){ String serviceAssessment =
	 * Validate.replaceHtml(request.getParameter("serviceAssessment");
	 * sdao.insertServiceAssessment(customerId, serviceAssessment);
	 * ArrayList<Object> serviceAssessmentList =
	 * sdao.selectServiceAssessment(customerId); if(serviceAssessmentList !=
	 * null){ for(int i=0;i<serviceAssessmentList.size();i++){ ServiceAssessment
	 * serviceAssessmentInfo = (ServiceAssessment)serviceAssessmentList.get(i);
	 * out.println(""+serviceAssessmentInfo.getAssessmentTime()+
	 * "&nbsp;&nbsp;&nbsp;&nbsp;���������۵���&nbsp;&nbsp;"
	 * +serviceAssessmentInfo.getServiceAssessment()+"<br/><br/>"); } } } }
	 * else{ ArrayList<Object> serviceAssessmentList =
	 * sdao.selectServiceAssessment(customerId);
	 * 
	 * request.setAttribute("serviceAssessmentList", serviceAssessmentList);
	 * request.setAttribute("goodsEvaluateList", goodsEvaluateList);
	 * request.setAttribute("orderList", orderList);
	 * request.setAttribute("customerDetailInfo", customerDetailInfo);
	 * request.setAttribute("customerInfo", customerInfo);
	 * request.getRequestDispatcher
	 * ("../filterWeb/myInformation.jsp").forward(request, response); } } }
	 */

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
